import 'package:demo/common/api/api_mixin.dart';
import 'package:demo/common/data/login_data.dart';
import 'package:demo/common/utils/dialog_util.dart';
import 'package:demo/common/utils/log_util.dart';
import 'package:demo/common/values/sp_keys.dart';
import 'package:demo/common/widgets/page/page_ctrl.dart';
import 'package:get/get.dart';

class LaunchCtrl extends GetxController with ApiMixin {
  // 泛型是列表数据的类型
  PageCtrl<String> pageCtrl = PageCtrl(
    startIndex: 1, // 默认1
    onRefresh: (pageCtrl, int index) async {
      await Future.delayed(Duration(seconds: 2));
      pageCtrl.addData(
        data: ["$index-1", "$index-2", "$index-3", "$index-4", "$index-5"],
        hasMore: index < 3, //是否还有下一页
      );
    },
  );

  @override
  void onReady() {
    super.onReady();
    pageCtrl.showLoading(); //会回调 onRefresh 并重置页面

    loadingScope(() async {
      await loginGuest();
    });

    Log.d("测试信息");

    Log.showDebugBtn(Get.context!);

    test();
  }

  @override
  void onClose() {
    pageCtrl.dispose();
    super.onClose();
  }

  test() async {
    Log.d("token=${SpKeys.token.getValue()}"); //显示""
    Log.d("test=${SpKeys.test.getValue()}"); //显示"1"
    Log.d("loginData=${SpKeys.loginData.getValue()}"); //显示"null"
    await SpKeys.token.setValue("testToken");
    await SpKeys.test.setValue(2);
    await SpKeys.loginData.setValue(LoginData(yonghuId: 0, pingju: "pingju111", xinyonghu: 221));
    Log.d("token=${SpKeys.token.getValue()}");
    Log.d("test=${SpKeys.test.getValue()}");
    Log.d("loginData=${SpKeys.loginData.getValue()}");
    //改变后缀 要小心key没有和其他key发生重叠
    SpKeys.testSuffix.setValue(1);
    SpKeys.testSuffix.defaultSuffix = "2";
    SpKeys.testSuffix.setValue(4);
    Log.d("testSuffix=${SpKeys.testSuffix.getAll()}");
    await SpKeys.testSuffix.removeAll();
    Log.d("testSuffix=${SpKeys.testSuffix.getAll()}");
  }
}
